package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 资讯功能实现类
 */
@Service
public class ArticleInfoServiceImpl extends ServiceImpl<ArticleInfoMapper, ArticleInfo> implements ArticleInfoService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的ArticleInfo表mapper对象
     */
    @Autowired
    private ArticleInfoMapper _ArticleInfoMpper;


    /**
     * 资讯分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<ArticleInfoDto> List(ArticleInfoPagedInput input) {

      
        //声明一个支持资讯查询的(拉姆达)表达式
        LambdaQueryWrapper<ArticleInfo> queryWrapper = Wrappers.<ArticleInfo>lambdaQuery()
                .eq(input.getId()!=null,ArticleInfo::getId,input.getId())
                .eq(input.getCreatorId()!=null,ArticleInfo::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入Author不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getAuthor())) {
            queryWrapper=queryWrapper.eq(ArticleInfo::getAuthor,input.getAuthor());
       	 }
     	 //如果前端搜索传入ArticleTypeId不为空,则进行精确查询
        if(input.getArticleTypeId()!=null) {
            queryWrapper=queryWrapper.eq(ArticleInfo::getArticleTypeId,input.getArticleTypeId());
       	 }
     	 //如果前端搜索传入Title不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getTitle())) {
            queryWrapper=queryWrapper.eq(ArticleInfo::getTitle,input.getTitle());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(ArticleInfo::getCreationTime);
        //构建一个分页查询的model
        Page<ArticleInfo> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取资讯数据
        IPage<ArticleInfo> pageRecords= _ArticleInfoMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _ArticleInfoMpper.selectCount(queryWrapper);

        //把ArticleInfo实体转换成ArticleInfo传输模型
        List<ArticleInfoDto> items= Extension.copyBeanList(pageRecords.getRecords(),ArticleInfoDto.class);
        for (ArticleInfoDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个资讯查询
     */
    @SneakyThrows
    @Override
    public ArticleInfoDto Get(ArticleInfoPagedInput input) {
       if(input.getId()==null)
        {
            return new ArticleInfoDto();
        }
        PagedResult<ArticleInfoDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new ArticleInfoDto();
    }

    /**
     *资讯创建或者修改
     */
    @SneakyThrows
    @Override
    public ArticleInfoDto CreateOrEdit(ArticleInfoDto input) {

        //声明一个资讯实体
        ArticleInfo ArticleInfo = new ArticleInfo();

        //把前端传入的input参数拷贝到资讯实体
        BeanUtils.copyProperties(ArticleInfo,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(ArticleInfo);

        //定义一个返回给前端的资讯传输模型
        ArticleInfoDto ArticleInfoDto=new ArticleInfoDto();

        //同理把操作的资讯实体拷贝给资讯传输模型
        BeanUtils.copyProperties(ArticleInfoDto,ArticleInfo);

        //把传输模型返回给前端
        return ArticleInfoDto;
    }



}
